package com.ruoyi.business.question;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.ObjectiveAnswer;
import com.ruoyi.business.domain.Question;
import com.ruoyi.business.domain.bo.QuestionBo;
import com.ruoyi.business.domain.vo.ObjectiveAnswerVo;
import com.ruoyi.business.domain.vo.QuestionVo;
import com.ruoyi.common.utils.bean.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 多选题的问题处理器
 * @author 最爱喝兽奶
 * @create 2022/8/9
 **/
public class MultipleChoiceQuestionHandle extends AbstractQuestionHandle implements QuestionHandle {

    public MultipleChoiceQuestionHandle() {
        super();
    }

    @Override
    public Question insertAnswer(QuestionBo bo, Question question) {
        String[] options = bo.getOptions().split(separator);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < options.length; i++) {
            String option = options[i];
            ObjectiveAnswer answer = new ObjectiveAnswer();
            answer.setContext(option);
            answer.setSort(String.valueOf(i));
            if (null == question.getId()) {
                throw new RuntimeException("问题插入失败， 无法插入答案字段");
            }
            answer.setQid(question.getId());
            objectiveAnswerMapper.insert(answer);
            char currentOption = (char) (65 + i);
            if (bo.getAnswerImport().contains(String.valueOf(currentOption))) {
                // 如果当前的答案是所选的话，就将其添加至问题字段中
                sb.append(answer.getId()).append(separator);
            }
        }
        question.setAnswer(sb.substring(0, sb.length() - 1));
        return question;
    }

    @Override
    public QuestionVo queryResult(Question question) {
        QuestionVo vo = new QuestionVo();
        BeanUtils.copyBeanProp(vo, question, true);
        // 设置单选题答案
        List<ObjectiveAnswer> answers = objectiveAnswerMapper.selectList(new QueryWrapper<ObjectiveAnswer>().eq("qid", vo.getId()));
        List<ObjectiveAnswerVo> collect = answers.stream().map(item -> {
            ObjectiveAnswerVo objectiveAnswerVo = new ObjectiveAnswerVo();
            BeanUtils.copyBeanProp(objectiveAnswerVo, item, true);
            if (vo.getAnswer().contains(item.getId())) {
                // 如果当前的答案是标准答案就给答案对象标记一下
                objectiveAnswerVo.setIsChoice(true);
                vo.getMultipleAnswer().add(item.getId());
            } else {
                objectiveAnswerVo.setIsChoice(false);
            }
            char key = (char) (65 + Integer.parseInt(item.getSort()));
            objectiveAnswerVo.setKey(key + "");
            return objectiveAnswerVo;
        }).collect(Collectors.toList());
        vo.setAnswers(collect);
        return vo;
    }

    @Override
    public void deleteQuestion(String id) {
        super.deleteQuestion(id);
        objectiveAnswerMapper.deleteAnswerByQuestionId(id);
    }

    @Override
    public Integer calcScore(String answer, String questionId, Integer score) {
        Question question = questionMapper.selectById(questionId);
        if (null != question) {
            String[] standardAnswers = question.getAnswer().split(";");
            String[] stuAnswers = answer.split(";");
            if (standardAnswers.length == stuAnswers.length) {
                for (int i = 0; i < standardAnswers.length; i++) {
                    for (int j = 0; j < stuAnswers.length; j++) {
                        if (stuAnswers[j].trim().equals(standardAnswers[i].trim())) {
                            break;
                        }
                        if (j == stuAnswers.length - 1) {
                            return 0;
                        }
                    }
                }
                if (null != score) {
                    return score;
                }
                return Integer.parseInt(question.getScore());
            }
        }
        return 0;
    }


    @Override
    public List<String> randQueryQuestionByType(String courseId, int limit) {
        return questionMapper.randQueryQuestionByType(courseId, QuestionHandleFactory.MULTIPLE_CHOICE_QUESTION, limit);
    }
}
